package com.kaifamiao.month03.day0317;

public class Queue {

    // 队列是一种特殊的线性表，只允许在表的前端进行删除操作，而在表的后端进行插入操作
    User[] users = new User[10];
    int count;

    public boolean add(User user){
        te(count >= users.length);
        users[count ++] = user;
        return true;
    }

    /**
     * 抛出异常
     * @param flag 判断条件
     */
    public void te(boolean flag){
        if(flag){
            throw new RuntimeException("异常");
        }
    }

    public boolean offer(User user){
        if(count >= users.length){
            return false;
        }
        users[count ++] = user;
        return true;
    }

    public User poll(){
        te(count == 0);
        User user = users[0];
        System.arraycopy(users,1,users,0,users.length - 1);
        count --;
        return user;
    }

    public User remove(){
        if(count == 0){
            return null;
        }
        User user = users[0];
        System.arraycopy(users,1,users,0,users.length - 1);
        count --;
        return user;
    }

    public User element(){
        te(count == 0);
        return users[0];
    }

    public User peek(){
        if(count == 0){
            return null;
        }
        return users[0];
    }

}
